package com.lzy.crm.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzy.crm.base.page.PageQuery;
import com.lzy.crm.base.util.BootStrapReturn;
import com.lzy.crm.base.util.JsonUtil;
import com.lzy.crm.base.util.StringUtil;
import com.lzy.crm.domain.User;
import com.lzy.crm.service.OrderService;
import com.lzy.crm.service.UserService;
import com.lzy.crm.util.BootJsonResponse;
import com.lzy.crm.util.UserConstants;
import com.lzy.crm.vo.OrderStatVo;
import com.lzy.crm.vo.OrderVo;

/**
 * 充值管理
 * @author csp
 *
 */
@Controller
@RequestMapping("/admin")
public class OrderInfoController {
	
	
	Logger logger= Logger.getLogger(OrderInfoController.class);
	
	@Resource
	UserService userService;
	
	@Resource
	OrderService orderService;
	
	
	
	@RequestMapping("/order_mobile")  
	public String tradeFlow(HttpServletRequest request,ModelMap model) {
		return "mobile/order/order_mobile";
	}

	@RequestMapping(path="/order_mobile_data",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String order_mobile_data(HttpServletRequest request, @RequestParam Map<String, String> form,
			ModelMap model) {
		BootStrapReturn<OrderVo> result = new BootStrapReturn<OrderVo>();
		try {
			String gameId = request.getParameter("gameUserId");
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String status = request.getParameter("status");
			if(StringUtil.isNotEmpty(startTime)){
				startTime+=" 00:00:00";
			}
			if(StringUtil.isNotEmpty(endTime)){
				endTime +=" 23:59:59";
			}
			User user = (User)request.getSession().getAttribute("userSession");
			if(!StringUtil.equals(user.getUserType(), UserConstants.USER_TYPE_MANAGE)){
				return null;
			}
			Integer currentPage = Integer.parseInt(request.getParameter("numPerPage"));
			Integer numPerPage = Integer.parseInt(request.getParameter("pageNum"));
			PageQuery pageQuery = new PageQuery(numPerPage,currentPage);
			pageQuery.setIsQueryTotal(true); 
			List<OrderVo> orderVoList = orderService.getOrderInfoList(gameId,status ,startTime, endTime, pageQuery);
			result.setPage(pageQuery.getPageIndex());
			result.setTotal(pageQuery.getTotalCount());
			result.setRows(orderVoList);
		} catch (Exception e) {
			logger.error("交易流水列表", e);
		}
		return JsonUtil.toJson(result) ;
	}
	
	@RequestMapping(path="/order_mobile_stat",method=RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String order_mobile_stat(HttpServletRequest request, @RequestParam Map<String, String> form,
			ModelMap model) {
		try {
			String gameId = request.getParameter("gameUserId");
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String status = request.getParameter("status");
			if(StringUtil.isNotEmpty(startTime)){
				startTime+=" 00:00:00";
			}
			if(StringUtil.isNotEmpty(endTime)){
				endTime +=" 23:59:59";
			}
			User user = (User)request.getSession().getAttribute("userSession");
			if(!StringUtil.equals(user.getUserType(), UserConstants.USER_TYPE_MANAGE)){
				return null;
			}
			OrderStatVo orderStatVo = orderService.getOrderStatVo(gameId,status ,startTime, endTime);
			model.put("statusCode", BootJsonResponse.STATUS_CODE_SUCCESS);
			model.put("orderStatVo", orderStatVo);
		} catch (Exception e) {
			logger.error("交易流水列表", e);
			model.put("statusCode", BootJsonResponse.STATUS_CODE_FAILURE);
		}
		return JsonUtil.toJson(model) ;
	}
	

}
